package controllers

import (
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"path/filepath"
	"strings"
	"time"
	"zc2/cache"
	"zc2/models"
)

type XsController struct {
	beego.Controller
}

func (c *XsController) Add() {
	book := c.GetString("book")
	author := c.GetString("author")
	num, _ := c.GetInt("num")
	//img := c.GetString("img")

	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 0,
			"msg":  "请选择要上传的文件",
		}
		c.ServeJSON()
		return
	}
	ext := map[string]bool{
		".png":  true,
		".jpg":  true,
		".jpeg": true,
	}
	suffix := strings.ToLower(filepath.Ext(header.Filename))
	if ext[suffix] == false {
		c.Data["json"] = map[string]interface{}{
			"code": 0,
			"msg":  "照片格式不对",
		}
		c.ServeJSON()
		return
	}
	if header.Size > 2*1024*1024 {
		c.Data["json"] = map[string]interface{}{
			"code": 0,
			"msg":  "照片大小不对",
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	path := "static/img/" + header.Filename
	c.SaveToFile("img", path)
	xs := models.Xs{
		Book:   book,
		Author: author,
		Num:    num,
		Img:    "http://127.0.0.1:8080/" + path,
	}
	err = models.DB.Debug().Create(&xs).Error
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 0,
			"msg":  "添加失败",
		}
		c.ServeJSON()
		return
	}
	key := fmt.Sprintf("xs_add_%d", xs.ID)
	marshal, _ := json.Marshal(&xs)
	cache.Rdb.Set(cache.Ctx, key, marshal, 0)

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "添加成功",
	}
	c.ServeJSON()
	return
}
func (c *XsController) Del() {
	id, _ := c.GetInt("id")
	var xs models.Xs
	xs.ID = uint(id)

	models.DB.Debug().Where("id=?", id).Delete(&xs)

	key := fmt.Sprintf("xs_add_%d", xs.ID)

	cache.Rdb.Del(cache.Ctx, key)

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "删除成功",
	}
	c.ServeJSON()
	return
}
func (c *XsController) Update() {
	id, _ := c.GetInt("id")
	book := c.GetString("book")
	author := c.GetString("author")
	num, _ := c.GetInt("num")
	img := c.GetString("img")

	xs := models.Xs{
		Book:   book,
		Author: author,
		Num:    num,
		Img:    img,
	}
	xs.ID = uint(id)
	err := models.DB.Debug().Where("id=?", id).Updates(&xs).Error
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 0,
			"msg":  "修改失败",
		}
		c.ServeJSON()
		return
	}
	key := fmt.Sprintf("xs_add_%d", xs.ID)
	marshal, _ := json.Marshal(&xs)
	cache.Rdb.Set(cache.Ctx, key, marshal, 0)

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "修改成功",
	}
	c.ServeJSON()
	return
}

type Res struct {
	Count int64
	Data  interface{}
}

func (c *XsController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)
	offset := (page - 1) * size
	var count int64
	models.DB.Model(&models.Xs{}).Count(&count)

	var allxs []models.LookData
	get, _ := cache.Rdb.Get(cache.Ctx, "xs_list").Result()
	if get != "" {
		json.Unmarshal([]byte(get), &allxs)
	} else {
		//models.DB.Debug().Offset(offset).Limit(size).Find(&allxs)
		models.DB.Model(&models.Look{}).Offset(offset).Limit(size).
			Select("looks.*, xs.book,xs.author,xs.num,xs.img").
			Joins("join xs on xs.lookid = looks.id").
			Find(&allxs)
		marshal, _ := json.Marshal(allxs)
		cache.Rdb.Set(cache.Ctx, "xslist", marshal, time.Minute*15)
	}
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "展示成功",
		"data": Res{
			Count: count,
			Data:  allxs,
		},
	}
	c.ServeJSON()
	return
}
